package com.limbo.showapp.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.limbo.showapp.R;
import com.limbo.showapp.adapter.CartoonAdapter;
import com.limbo.showapp.base.BaseActivity;
import com.limbo.showapp.bean.CartoonBean;
import com.limbo.showapp.bean.HotBean;
import com.limbo.showapp.constant.AppConstant;
import com.limbo.showapp.presenter.CartoonPresenter;
import com.limbo.showapp.util.DisplayUtils;
import com.limbo.showapp.util.MyUtils;
import com.limbo.showapp.util.SPUtils;
import com.limbo.showapp.util.ToastUtils;
import com.limbo.showapp.view.IRefreshView;
import com.limbo.showapp.widget.SimpleOptionView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;

/**
 * Created by wangqi on 2018/9/6.
 */

public class CartoonActivity extends BaseActivity<IRefreshView, CartoonPresenter> implements IRefreshView<CartoonBean>,
        OnRefreshLoadMoreListener {

    private SimpleOptionView simpleTileView;
    private SmartRefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private CartoonAdapter adapter;

    @Override
    protected CartoonPresenter getPresenter() {
        return new CartoonPresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_cartoon;
    }

    @Override
    protected void initView() {
        simpleTileView = findViewById(R.id.simpleTileView);
        refreshLayout = findViewById(R.id.refreshLayout);
        recyclerView = findViewById(R.id.recyclerView);

        //沉浸式
        DisplayUtils.initAfterSetContentView(this, simpleTileView);

        simpleTileView.setTitle("漫画");
        refreshLayout.setOnRefreshLoadMoreListener(this);
        refreshLayout.setEnableAutoLoadMore(false);//是否启用列表惯性滑动到底部时自动加载更多
        refreshLayout.setEnableOverScrollDrag(true);//是否启用越界拖动（仿苹果效果）1.0.4
        refreshLayout.setDisableContentWhenRefresh(true);//是否在刷新的时候禁止列表的操作
        refreshLayout.setDisableContentWhenLoading(true);//是否在加载的时候禁止列表的操作

        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        adapter = new CartoonAdapter(this);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                CartoonDispalyActivity.OpenActivity(mContext, AppConstant.Url.BASE_URL + adapter.getDatas().get(position).getSourceUrl());
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }

    @Override
    protected void initData() {
        CartoonBean cartoonBean = (CartoonBean) SPUtils.getObject(this, AppConstant.SP.DATA_LIST_CARTOON);
        setCartoonData(cartoonBean);

        //第一次获取数据
        refreshLayout.autoRefresh();
    }

    @Override
    public void onRefreshData(CartoonBean cartoonBean) {
        SPUtils.setObject(this, AppConstant.SP.DATA_LIST_CARTOON,cartoonBean);

        setCartoonData(cartoonBean);
    }

    private void setCartoonData(CartoonBean cartoonBean) {
        if (cartoonBean==null)
            return;
        adapter.setData(cartoonBean.getData());
        refreshLayout.finishRefresh();
        refreshLayout.setEnableLoadMore(true);
    }

    @Override
    public void onLoadMoreData(CartoonBean cartoonBean) {
        adapter.addData(cartoonBean.getData());
        refreshLayout.finishLoadMore();
        if (cartoonBean.getData().size() < mPresenter.size) {
            refreshLayout.setEnableLoadMore(false);
            ToastUtils.getInstanc(this).showToast("无更多数据");
        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        mPresenter.loadMoreData();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mPresenter.refreshData();
    }

    @Override
    public void onError(String msg) {

    }

    /**
     *
     * @param context
     */
    public static void OpenActivity(Context context) {
        Intent intent = new Intent(context, CartoonActivity.class);
        context.startActivity(intent);
    }
}
